package com.cloudbroker.bcs.common.util;

import java.util.List;

public abstract class SessionHelper {
    
    protected abstract CacheHelper getCacheHelper();
    
    protected abstract int getSessionTimeout();
    
    protected abstract List<String> getSessionPrefixs();
    
    /**
     * 保存信息到会话
     * @param key
     * @param info 信息
     */
    public void setInfoForSession(String key, String info) {
        setInfoForSession(key ,info , getSessionTimeout());
    }

    public void setInfoForSession(String key, String info,int sessionTimeout) {
        getCacheHelper().set(key, info, sessionTimeout);
    }
    
    /**
     * 刷新会话
     * @param access_token
     */
    public void refreshSession(String access_token) {
        int expireSecs = getSessionTimeout();
        CacheHelper cacheHelper = getCacheHelper();
        List<String> sessionPrefixs = getSessionPrefixs();
        for (String sessionPrefix : sessionPrefixs) {
            cacheHelper.refresh(sessionPrefix + access_token, expireSecs);
        }
    }
    
    /**
     * 刷新会话
     * @param access_token
     */
    public void destroySession(String access_token) {
        CacheHelper cacheHelper = getCacheHelper();
        List<String> sessionPrefixs = getSessionPrefixs();
        for (String sessionPrefix : sessionPrefixs) {
            cacheHelper.destroy(sessionPrefix + access_token);
        }
    }
    
    /**
     * 根据key获取保存的信息
     * @param key
     * @return 对应的信息或者null
     */
    public String fetchInfoByKey(String key) {
        return getCacheHelper().get(key);
    }

    /**
     * 根据key删除保存的信息
     * @param key
     */
    public void delete(String key) {
        getCacheHelper().delete(key);
    }
    
}
